<?php

namespace app\index\controller;

use app\index\model\Article;
use app\index\model\Collect;
use app\index\model\Comment;
use app\index\model\Fake as ModelFake;
use app\index\model\Follow;
use app\index\model\User;
use app\index\validate\User as ValidateUser;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Filesystem;

class Account extends Base
{
    public function index()
    {
        $this->logs('个人账户视图');
        $userData =  (new User())->profile($this->user['user_id']);
        return view('index', ['userData' => $userData]);
    }
    public function artList()
    {
        $this->logs('个人账户文章列表');
        $title = input('title');
        $limit = input('limit') ?? 10;
        $articleList = (new Article())->list(['user_id' => $this->user['user_id'], 'title' => $title, 'limit' => $limit]);
        return success($articleList, '获取成功', 0);
    }
    // 登录用户的空间
    public function article()
    {
        $this->logs('个人账户文章视图');
        return view('art_list');
    }
    public function comment()
    {
        $this->logs('个人账户评论');
        $commentList = (new comment())->list(['user_id' => $this->user['user_id']]);
        $userData =  (new User())->profile($this->user['user_id']);
        $page = $commentList['list']->render();
        return view('comment_list', ['data' => $userData, 'commentList' => $commentList, 'page' => $page]);
    }
    public function collect()
    {
        $this->logs('个人账户收藏');
        $collectList = Collect::list($this->user['user_id']);
        $userData =  (new User())->profile($this->user['user_id']);
        $page = $collectList['list']->render();
        return view('collect_list', ['data' => $userData, 'collectList' => $collectList, 'page' => $page]);
    }
    public function update()
    {
        return view();
    }
    // 获取用户的基本信息
    public function info()
    {
        $param = input();
        $id = $param['uid'];
        $data = User::field('id ,username , nickname , email ,head_icon, signature,create_time ')->where('id', $id)->find();
        return success($data);
    }
    // 个人信息修改
    public function doUpdate()
    {
        $param = input('post.');

        if (!empty(@$param['scene']) && request()->isPost()) {
            if ($param['scene'] == 'update') {
                $data = ['nickname' => $param['nickname'], 'email' => $param['email']];
            } else if ($param['scene'] == 'changePwd') {
                $data = ['password' => $param['password'], 'newPassword' => $param['newPassword']];
            } else {
                return error();
            }
            try {
                validate(ValidateUser::class)->scene($param['scene'])->check($data);
                $userData = User::find($this->user['user_id']);
                if ($param['scene'] == 'changePwd') {
                    if ($param['newPassword'] != $param['confirmPwd']) return error('两次密码输入不一致');
                    $oldPwd = $userData->password;
                    $data['password'] = md5($param['newPassword']);
                    if ($oldPwd != md5($param['password']))  return error('原密码错误');
                    if ($oldPwd == md5($param['newPassword']))  return error('新密码不能与旧密码一样');
                    unset($data['newPassword']);
                    unset($data['confirmPwd']);
                    $res = User::where(['id' => $this->user['user_id']])->update($data);
                    if ($res) return success();
                }
                if($userData->nickname != $param['nickname'])
                {
                    $hasNickname = Db::name('user')->where('nickname' , $param['nickname'])->find();
                    if($hasNickname) return error('已存在该昵称，请更换一个吧！');
                }
                unset($param['scene']);
                $res = User::where(['id' => $this->user['user_id']])->update($param);
                if ($res) return success();
                return error();
            } catch (ValidateException $e) {
                return error($e->getError());
            }
        }
    }
    // 上传
    public function uploadHeadIcon()
    {
        $images = request()->file();
        try {
            validate(['headIcon' => 'fileSize:10240|fileExt:jpg,png,jpeg'])
                ->check($images);
            $path = Filesystem::disk('images')->putFile('headIcon', $images['headIcon']);
            $path = str_replace('\\', '/', $path);
            $path = "/static/images/" . $path;
            $res = User::where('id', $this->user['user_id'])->update(['head_icon' => $path]);
            if ($res) return success(['url' => $path]);
            return error();
        } catch (\think\exception\ValidateException $e) {
            return  error($e->getMessage());
        }
    }
   
    public function useFake()
    {
        if (request()->isPost()) {
            $fake = input('fake');
            if (!empty($fake)) {
                return  ModelFake::use($fake, $this->user['user_id']);
            }
        }
    }
    public function follow()
    {
        $list = Follow::list($this->user['user_id']);
        $page = $list->render();
        return view('follow_list',['list' => $list , 'page' => $page]);
    }
}
